// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#include <kllm/kai/metrics.h>

namespace kllm {

    void KaiMetrics::metrics(const KaiRequest &req, KaiResponse&res) {
        if (!_context->params.endpoint_metrics) {
            res.mutable_status()->set_code(ERROR_TYPE_NOT_SUPPORTED);
            res.mutable_status()->set_errmsg("This server does not support metrics endpoint. Start it with `--metrics`");
            return;
        }

        if(req.query_type() != QUERY_METRICS) {
            res.mutable_status()->set_code(ERROR_TYPE_INVALID_REQUEST);
            res.mutable_status()->set_errmsg("internal error for request metrics");
            return;
        }
        res.set_model_alias(_context->params.model_alias);
        // request slots data using task queue
        server_task task;
        task.id = _context->queue_tasks.get_new_id();
        task.id_target = -1;
        task.type = SERVER_TASK_TYPE_METRICS;
        task.reset_bucket = true;

        _context->queue_results.add_waiting_task_id(task.id);
        _context->queue_tasks.post(task, true); // high-priority task

        // get the result
        TaskResult result = _context->queue_results.recv(task.id);
        _context->queue_results.remove_waiting_task_id(task.id);
        *res.mutable_metrics() = result.metrics;
        res.mutable_status()->set_code(0);
        res.mutable_status()->set_errmsg("ok");
        res.set_query_type(QUERY_METRICS);
    }

    void KaiMetrics::slots(const KaiRequest &req, KaiResponse&res) {
        if (!_context->params.endpoint_metrics) {
            res.mutable_status()->set_code(ERROR_TYPE_NOT_SUPPORTED);
            res.mutable_status()->set_errmsg("This server does not support metrics endpoint. Start it with `--metrics`");
            return;
        }

        if(req.query_type() != QUERY_METRICS) {
            res.mutable_status()->set_code(ERROR_TYPE_INVALID_REQUEST);
            res.mutable_status()->set_errmsg("internal error for request metrics");
            return;
        }

        // request slots data using task queue
        server_task task;
        task.id = _context->queue_tasks.get_new_id();
        task.id_target = -1;
        task.type = SERVER_TASK_TYPE_METRICS;
        task.reset_bucket = true;

        _context->queue_results.add_waiting_task_id(task.id);
        _context->queue_tasks.post(task, true); // high-priority task

        // get the result
        TaskResult result = _context->queue_results.recv(task.id);
        _context->queue_results.remove_waiting_task_id(task.id);
        res.mutable_slot_state()->Assign(result.slot_state.begin(), result.slot_state.end());
        res.mutable_status()->set_code(0);
        res.mutable_status()->set_errmsg("ok");
        res.set_query_type(QUERY_METRICS);
    }
}  // namespace kllm
